package com.hyxt.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hyxt.api.ChainLinkServer;
import com.hyxt.handler.SlaveHandler;
import com.hyxt.utils.Caches;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author songm
 * @version v1.0
 * @Description 接收从链路809客户端消息
 * @Date: Create in 18:32 2017/12/19
 * @Modifide By:
 **/
@Component("receive809ClientMsg")
public class Receive809ClientMsg implements IReceiveMsg {

    //809链路管理
    @Reference(version = "1.0.0")
    private ChainLinkServer chainLinkServer;
    @Override
    public Boolean listen(final String channelName) {
        if (channelName != null) {
            //开启一个线程持续监听通道
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        //String message = RedisUtil.singleListener(channelName);
                        // todo 调用链路管理监听接口
                        String message = chainLinkServer.receiveMsg(channelName);
                        SlaveHandler handler = (SlaveHandler) Caches.clientHandlers.get(channelName);
                        if (handler == null){
                            continue;
                        }
                        handler.setSlaveChannel(channelName);
                        handler.process(message);
                    }
                }
            }).start();
            return true;
        }
        return false;
    }
}
